package com.sinevil.tcpTrans.tcp;

import java.io.IOException;
import java.net.InetAddress;
import java.nio.channels.AlreadyBoundException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.UnsupportedAddressTypeException;
import java.security.NoSuchAlgorithmException;

/**
 * @Classname TCPServer
 * @Description TODO
 * @Version 1.0.0
 * @Date 2025/1/11 17:25
 * @Created by sinevil
 */
public class TCPServer extends TCPServerNode{

    /**
     * 创建一个TCP服务器
     * @param serverPort 服务器端口
     * @throws NoSuchAlgorithmException 当不支持MD5算法时抛出
     * @throws AlreadyBoundException 如果套接字已经被绑定
     * @throws UnsupportedAddressTypeException 如果不支持给定地址的类型
     * @throws ClosedChannelException 如果通道关闭
     * @throws IOException 如果发生其他I/O错误
     * @throws SecurityException 如果已安装安全管理器，并且它拒绝该操作
     */
    public TCPServer(int serverPort) throws NoSuchAlgorithmException, IOException {
        super(serverPort);
    }

    public String getIPAndPort() {
        InetAddress inetAddress = serverSocketChannel.socket().getInetAddress();
        inetAddress.getHostName();
        return inetAddress.getCanonicalHostName()+":"+serverSocketChannel.socket().getLocalPort();
    }
}